package entidades;

import util.Util;
import util.MySQL;
import java.util.Map;
import java.util.List;
import java.util.HashMap;
import java.util.LinkedList;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.google.gson.Gson;

/**
 *
 * @author JFM
 */
public class FormatosPorNewlong extends Entidad
	{
	private String proporcion;
	private long cantidad;
	private int newlongId;
	private int formatoId;
	private int orden;

	private Newlong newlong;
	private Formato formato;

	static final String NOMBRE_TABLA = "formatos_x_newlong";



	// --- Constructores --- //

	public FormatosPorNewlong()
		{
		id = 0;
		proporcion = "0";
		orden = 0;
		cantidad = 0;

		newlongId = 0;
		newlong = null;
		formatoId = 0;
		formato = null;
		}

	public FormatosPorNewlong(int id) throws SQLException
		{ super(id); }

	public FormatosPorNewlong(String id) throws SQLException
		{ super(id); }

	public FormatosPorNewlong(ResultSet result) throws SQLException
		{ super(result); }



	// --- Metodos get()/set() --- //

	public int getOrden()
		{ return orden; }
	public void setOrden(int orden)
		{ this.orden = orden; }
	public void setOrden(String orden)
		{ try{ this.orden = Integer.parseInt(orden); }catch(Exception e) {} }

	public String getProporcion()
		{ return proporcion; }
	public String getProporcionToString()
		{
		if(proporcion.length() > 3)
			{
			String a2 = proporcion.substring(proporcion.length() - 1, proporcion.length());
			String a1 = proporcion.substring(proporcion.length() - 2, proporcion.length()-1);

			if(a2.compareTo("0") == 0)
				{
				if(a1.compareTo("0") == 0)
					{ return proporcion.substring(0, proporcion.length()-3); }
				else
					{ return proporcion.substring(0, proporcion.length()-1).replace(".", ","); }
				}
			}

		return proporcion.replace(".", ",");
		}
	public long getCantidad(long cantidadTotal)
		{
		long cantidadT = 0;
		try{ cantidadT = (long) ( (cantidadTotal * Float.parseFloat(proporcion)) / 100.0f); }catch(Exception e) {}
		return cantidadT;
		}
	public void setProporcion(String proporcion)
		{ this.proporcion = proporcion; }

	public long getCantidad()
		{ return cantidad; }
	public void setCantidad(long cantidad)
		{ this.cantidad = cantidad; }
	public void setCantidad(String cantidad)
		{ try{ this.cantidad = Long.parseLong(cantidad); }catch(Exception e) {} }

	public int getNewlongId()
		{ return newlongId; }
	public void setNewlongId(int newlongId)
		{ this.newlongId = newlongId; }
	public void setNewlongId(String newlongId)
		{ try{ this.newlongId = Integer.parseInt(newlongId); }catch(Exception e) {} }

	public Newlong getNewlong() throws SQLException
		{
		if(newlong == null || newlongId != newlong.getId())
			{ newlong = new Newlong(newlongId); }
		return newlong;
		}

	public Formato getFormato() throws SQLException
		{
		if(formato == null || formatoId != formato.getId())
			{ formato = new Formato(formatoId); }
		return formato;
		}

	public int getFormatoId()
		{ return formatoId; }
	public void setFormatoId(int formatoId)
		{ this.formatoId = formatoId; }
	public void setFormatoId(String formatoId)
		{ try{ this.formatoId = Integer.parseInt(formatoId); }catch(Exception e) {} }

	@Override
	public String getDescriptor()
		{ return id+""; }

	@Override
	public String toJson()
		{
		HashMap map = new HashMap();
		map.put("id", id);
		map.put("orden", orden);
		map.put("proporcion", proporcion);
		map.put("newlong_id", newlongId);
		map.put("formato_id", formatoId);
		return new Gson().toJson(map);
		}



	// --- Metodos MySQL --- //

	@Override
	public String getNombreTabla()
		{ return NOMBRE_TABLA; }

	@Override
	public void leer(ResultSet result, String prefijo) throws SQLException
		{
		setId(result.getInt(prefijo + "id"));
		setOrden(result.getByte(prefijo + "orden"));
		setProporcion(result.getString(prefijo + "proporcion"));
		setCantidad(result.getString(prefijo + "cantidad"));
		setFormatoId(result.getInt(prefijo + "formato_id"));
		setNewlongId(result.getInt(prefijo + "newlong_id"));
		}

	@Override
	public void cargar() throws SQLException
		{
		String query = "SELECT * FROM " + NOMBRE_TABLA + " WHERE ";
		query += (id != 0)? ("id = " + id) : ("newlong_id = " + newlongId + " AND formato_id = " + formatoId);

		ResultSet result = MySQL.executeQuery(query);
		if(result.next()) { leer(result); }

		result.getStatement().close();
		}
	
	@Override
	public void guardar() throws SQLException
		{
		ResultSet result = MySQL.executeQuery("Call FormatosPorNewlong_Assign(" +
				MySQL.formatId(id) + "," +
				MySQL.format(orden) + "," +
				MySQL.format(proporcion) + "," +
				MySQL.format(cantidad) + "," +
				MySQL.formatId(newlongId) + "," +
				MySQL.formatId(formatoId) + ")");

		if(result.next()) { leer(result); }

		result.getStatement().close();
		}

	public static void borrar(String id) throws SQLException
		{ Entidad.borrar(id, NOMBRE_TABLA); }

	public static List<FormatosPorNewlong> listar() throws SQLException
		{ return listar(0); }

	public static List<FormatosPorNewlong> listar(int newlongId) throws SQLException
		{ return listar(newlongId, false); }
	
	public static List<FormatosPorNewlong> listar(int newlongId, boolean porOrden) throws SQLException
		{
		LinkedList<FormatosPorNewlong> lista = new LinkedList();
		
		String query = "SELECT * FROM formatos_x_newlong_view";
		
		if(newlongId == 0)
			{ query += " ORDER BY newlong_id ASC, ancho ASC, largo ASC, altura ASC"; }
		else
			{
			query += " WHERE newlong_id = " + newlongId + " ORDER BY ";
			query += (porOrden)? "orden ASC" : "inicial ASC, ancho ASC, largo ASC, altura ASC";
			}
		
		ResultSet result = MySQL.executeQuery(query);

		while(result.next())
			{
			FormatosPorNewlong dpn = new FormatosPorNewlong();
			dpn.leer(result);
			lista.add(dpn);
			}
		result.getStatement().close();
		
		return lista;
		}
	
	public static List<Map> getOrden(int newlongId) throws SQLException
		{
		LinkedList<Map> lista = new LinkedList();	
		ResultSet result = MySQL.executeQuery("SELECT formato_id, inicial, ancho, largo, altura FROM formatos_x_newlong_view WHERE newlong_id = " + newlongId + " ORDER BY orden ASC");

		while(result.next())
			{
			HashMap map = new HashMap();
			map.put("formato_id", result.getInt("formato_id"));
			map.put("formato", result.getString("inicial") + result.getFloat("ancho") + "x" + result.getFloat("largo") + "x" + result.getFloat("altura"));
			lista.add(map);
			}
		result.getStatement().close();
		
		return lista;
		}
	
	public static void cambiarOrden(int newlongId, String formatoId, String orden) throws SQLException
		{ MySQL.executeUpdate("UPDATE formatos_x_newlong SET orden = " + orden + " WHERE newlong_id = " + newlongId + " AND formato_id = " + formatoId); }
	
	public static void cambiarProporcionYCantidad(int newlongId, int formatoId, String proporcion, String cantidad) throws SQLException
		{ 
                MySQL.executeUpdate("UPDATE formatos_x_newlong SET proporcion = " + proporcion + ", cantidad = " + cantidad +" WHERE newlong_id = " + newlongId + " AND formato_id = " + formatoId);
               }
        public static FormatosPorNewlong getFormatoNewlong(int newolongId, int formatoId) throws SQLException{
                FormatosPorNewlong fxn = null;
                String query = "SELECT * FROM formatos_x_newlong WHERE ";
		query += "formato_id = " + formatoId + " AND ";
		query += "newlong_id = " + newolongId ;

                 ResultSet result = MySQL.executeQuery(query);
		if(result.next()) {
                    fxn = new FormatosPorNewlong(result.getInt("id"));
                }
		result.close();

		return fxn;

            }

	}
